package com.gobi.provider;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Porter.YY
 * 2020/1/3.
 */
@RestController
public class ProviderApiController {

    @Value("${server.port}")
    String port;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("discoveryClient")
    public void discoveryClient() {
        List<String> services = discoveryClient.getServices();
        services.forEach((serviceInstance) -> {
            discoveryClient.getInstances(serviceInstance)
                    .forEach((t) -> {
                        System.out.println(serviceInstance + "\r\n" + JSON.toJSONString(t));
                    });
        });
    }

    @GetMapping("/provider")
    public String provider(@RequestParam(value = "name", defaultValue = "gobi") String name) {
        return "Provider: [" + name + "] ,i am from port:" + port;
    }

    @GetMapping("/getUser")
    public User getUser(@RequestParam(value = "name", defaultValue = "gobi") String name) {
        return new User("张三", 23);
    }

    @GetMapping("/void")
    public void returnNull() {
        System.out.println("返回为空，feign怎么解码");
        throw new NullPointerException("返回为空，feign怎么解码");
    }

    @GetMapping("/void2")
    public String returnNull2() {
        System.out.println("返回为空，feign怎么解码");
        return null;
    }
}
